package com.pat.java.tools;

import java.net.URL;
import java.util.Map;

import com.chinatricom.util.Encrypt;
import com.chinatricom.util.MD5;


public class HttpDemo {
	public static void main(String[] args) {
		//发送
		String sendURL = "http://ems.smsway.com.cn/sendSMS.action";
		//短信接收
		String recURL = "http://ems.smsway.com.cn/getSmsMo.action";
		//查询状态
		String fetchReportURL = "http://ems.smsway.com.cn/getSmsReport.action";
		//查询余额
		String fetchBalanceURL = "http://ems.smsway.com.cn/getSmsBalance.action";
		
		//查询关键字
		String findFiltratesURL = "http://ems.smsway.com.cn/getSmsFiltrates.action";
		
		//企业id
		String enterpriseID = "13580";
		//登录名
		String loginName = "admin";
		//密码
		String password = "wtmk#x1152";
		//消息id
		String smsId = "";
		//扩展端口
		String subPort = "";
		//内容 
		String content = "春宵一刻值千金【小桂圆】";
		//号码
		String mobiles = "18689499406";
		
		//定时发送时间
		String sendTime = "";
		
		//post内容
		String postContent = "";
		//返回结果
		String returnXML = "";
		
		//解释返回结果
		Map map = null;
		try {
			long beginT = System.currentTimeMillis();
			long e = System.currentTimeMillis();
			for(int i = 0 ; i < 1; i ++){
				//发送
				e = System.currentTimeMillis();
				
				postContent = "enterpriseID=" + enterpriseID + "&loginName=" + loginName + "&password=" +  Encrypt.MD5Encode(password) + "&smsId=" + smsId + "&subPort=" + subPort + "&content=" + java.net.URLEncoder.encode(content, "UTF-8") + "&mobiles=" + mobiles + "&sendTime=" +sendTime;
				returnXML = HttpUtil.httpPostRequest(new URL(sendURL), postContent);
				System.out.println("sendURL returnXML = " + returnXML);
				
//				//解释xml结果
//				map = XmlUtil.getNodeValues(XmlUtil.getRootElement(returnXML));
//				String result = (String)map.get("Result");    //应用时判断，若该值为0表示成功往下取smsid,若不为0则是相应错误代码。
//				String SmsId = (String)map.get("SmsId");
//				System.out.println("result = " + result + " SmsId = " + SmsId);
//				System.out.println("第" + i + ",time = " + (System.currentTimeMillis()- e)); 
				  
			}
			System.out.println("all over time = " + (System.currentTimeMillis()- beginT)); 


			
//			//短信接收
//			postContent = "enterpriseID=" + enterpriseID + "&loginName=" + loginName + "&password=" +  MD5.toMD5(password);
//			returnXML = HttpUtil.httpPostRequest(new URL(recURL), postContent);
//			System.out.println("recURL returnXML = " + returnXML);
//			//解释返回xml结果
//			map = XmlUtil.getNodeValues(XmlUtil.getRootElement(returnXML));
//			String result = (String)map.get("Result");
//			if("0".equals(result)){
//				//解释上行内容
//				Element moElement = XmlUtil.getRootElement(returnXML).element("Mo");
//				int num = Integer.parseInt(moElement.attributeValue("num"));
//				System.out.println("mo size = " + num);
//				Element item = null;
//				List list = moElement.elements();
//				
//				for (int i = 0; i < list.size(); i++) {
//					item = (Element)list.get(i);  //每一个上行item行
//					System.out.println("id = " + item.attributeValue("id") + " content = " + item.attributeValue("content") + " from_mobile = " + item.attributeValue("from_mobile") + " to_port = " + item.attributeValue("to_port") + " rec_time = " + item.attributeValue("rec_time"));
//				}
//			}
//			System.out.println("time = " + (System.currentTimeMillis()- beginT)); 	
			
			
//			//查询状态
//			postContent = "enterpriseID=" + enterpriseID + "&loginName=" + loginName + "&password=" +  MD5.toMD5(password);
//			returnXML = HttpUtil.httpPostRequest(new URL(fetchReportURL), postContent);
//			System.out.println("fetchReportURL returnXML = " + returnXML);	
//			//解释返回xml结果
//			map = XmlUtil.getNodeValues(XmlUtil.getRootElement(returnXML));
//			String result = (String)map.get("Result");
//			if("0".equals(result)){
//				//解释上行内容
//				Element reportElement = XmlUtil.getRootElement(returnXML).element("Report");
//				int num = Integer.parseInt(reportElement.attributeValue("num"));
//				System.out.println("report size = " + num);
//				Element item = null;
//				List list = reportElement.elements();
//				
//				for (int i = 0; i < list.size(); i++) {
//					item = (Element)list.get(i);  //每一个item状态报告
//					System.out.println("smsId = " + item.attributeValue("smsId") + " to_mobile = " + item.attributeValue("to_mobile") + " status = " + item.attributeValue("status") + " reportTime = " + item.attributeValue("reportTime") + " desc = " + item.attributeValue("desc"));
//				}
//			}			
//			System.out.println("time = " + (System.currentTimeMillis()- beginT)); 	
//			
			
//			//查询余额
//			postContent = "enterpriseID=" + enterpriseID + "&loginName=" + loginName + "&password=" +  MD5.toMD5(password);
//			returnXML = HttpUtil.httpPostRequest(new URL(fetchBalanceURL), postContent);
//			System.out.println("fetchBalanceURL returnXML = " + returnXML);
//			//解释xml结果
//			map = XmlUtil.getNodeValues(XmlUtil.getRootElement(returnXML));
//			String result = (String)map.get("Result");    
//			String Balance = (String)map.get("Balance");
//			System.out.println("result = " + result + " Balance = " + Balance);
//			System.out.println("time = " + (System.currentTimeMillis()- beginT)); 		
			
//			//查询关键字
//			postContent = "enterpriseID=" + enterpriseID + "&loginName=" + loginName + "&password=" +  MD5.toMD5(password) + "&content=" + java.net.URLEncoder.encode(content, "UTF-8");
//			returnXML = HttpUtil.httpPostRequest(new URL(findFiltratesURL), postContent);
//			System.out.println("findFiltratesURL returnXML = " + returnXML);
//			//解释xml结果
//			map = XmlUtil.getNodeValues(XmlUtil.getRootElement(returnXML));
//			String result = (String)map.get("Result");    
//			String Filtrates = (String)map.get("Filtrates");
//			System.out.println("result = " + result + " Filtrates = " + Filtrates);			
//			System.out.println("time = " + (System.currentTimeMillis()- beginT)); 
//			
//			
		} catch (Exception ex) {
			// TODO: handle exception
			System.out.println("ERROR:" + ex.getMessage());
		}
	}
	
}
